package com.zhu.rpc.server;

import com.zhu.rpc.server.handler.RpcMsgDecode;
import com.zhu.rpc.server.handler.RpcProtocolDecodeHandler;
import com.zhu.rpc.server.handler.RpcHandler;
import com.zhu.rpc.server.handler.ServerHeartBeatHandler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;


public class RpcServerChannelInitializer extends ChannelInitializer<Channel> {
    private static final String String_Decode = "stringDecode";
    private static final String Json_Decode = "jsonDecode";
    private static final String String_Encode = "stringEncode";
    private static final String Rpc_Handler = "rpcHandler";

    @Override
    protected void initChannel(Channel ch) throws Exception {
        addRpcHandler(ch.pipeline());
    }

    private void addRpcHandler(ChannelPipeline pipeline) {
        pipeline.addLast("idleStateHandler", new IdleStateHandler(65, 0, 60*10, TimeUnit.SECONDS));
        pipeline.addLast("serverHeartBeatHandle", new ServerHeartBeatHandler());
        pipeline.addLast(String_Encode,new StringEncoder());
        pipeline.addLast("msgDecode",new RpcMsgDecode());
//        pipeline.addLast(String_Decode,new StringDecoder());
        pipeline.addLast(Json_Decode,new RpcProtocolDecodeHandler());
        pipeline.addLast(Rpc_Handler,new RpcHandler());
    }
}
